import axios from 'axios'
const url = 'https://api.xinyew.cn/api/yanxuantc'
// 上传文件的函数
export const requestMethod = async ({
  file,
  data,
  headers,
  withCredentials,
  action = url,
  onFinish,
  onError,
  onProgress,
}: any) => {
    const formData = new FormData()
    const filename = file?.file ? file.file : file
    formData.append('file', filename as any)
  try {
    const response: any = await axios.post(action, formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
      onUploadProgress: (progressEvent) => {
        const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
        if (onProgress) {
          onProgress({ percent: percentCompleted })
        }
      },
    })
    if (response.errno == 1) {
      return {
        status: 'failed',
        error: response.error,
      }
    }
    return {
      status: 'success',
      data: { ...response.data.data },
    }
  } catch (error: any) {
    return {
      status: 'failed',
      error: error.error,
    }
  }
}
